package cn.fl.riskctrl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.riskctrl.service.IRcCstLivLstService;
import cn.fl.riskctrl.transform.RskCtrlCfgMVOTransform;
import cn.fl.riskctrl.vo.RskCtrlCfgMVO;
import cn.fl.system.service.ISysDictionaryService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlAddApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlModifyApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlRemoveApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlSearchApiService;
import fl.riskctrl.facade.riskctrl.dto.RskCtrlCfgMDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fl.riskctrl.facade.riskctrl.dto.RcCstLivLstDTO;
import cn.fl.riskctrl.transform.RcCstLivLstVOTransform;
import cn.fl.riskctrl.vo.RcCstLivLstVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.base.framework.utils.BaseLogUtils;

/**
 * @Description: 风险控制活体检测名单表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class RcCstLivLstServiceImpl implements IRcCstLivLstService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IRcCstLivLstService.class);

	@Autowired
	private IRiskCtrlSearchApiService riskCtrlSearchApiService;

	@Autowired
	private IRiskCtrlAddApiService riskCtrlAddApiService;

	@Autowired
	private IRiskCtrlModifyApiService riskCtrlModifyApiService;

	@Autowired
	private IRiskCtrlRemoveApiService riskCtrlRemoveApiService;

	@Autowired
	private ISysDictionaryService sysDictionaryService;

	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Override
	public RcCstLivLstVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		RcCstLivLstDTO dto = this.riskCtrlSearchApiService.getRcCstLivLstById(id);
		return RcCstLivLstVOTransform.toVO(dto);
	}

	@Override
	public Long add(RcCstLivLstVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.riskCtrlAddApiService.addRcCstLivLst(RcCstLivLstVOTransform.toDTO(entity));
		//TODO 业务逻辑
//		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.riskCtrlRemoveApiService.removeRcCstLivLstById(id);
		//TODO 业务逻辑
//		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(RcCstLivLstVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.riskCtrlModifyApiService.modifyRcCstLivLst(RcCstLivLstVOTransform.toDTO(entity));
		//TODO 业务逻辑
//		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		Pager pager = this.riskCtrlSearchApiService.searchRcCstLivLstListPage(page);
		List<RcCstLivLstDTO> resultList = (List<RcCstLivLstDTO>) pager.getResultList();
		//数据字典（系统名+是否启用）
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_LIV_STS_CD);
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_LIV_TYP_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> sysList = dicMap.get(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		List<KeyValueVO> livstsList = dicMap.get(DictionaryConstant.CODE_GRP_LIV_STS_CD);
		List<KeyValueVO> livtypList = dicMap.get(DictionaryConstant.CODE_GRP_LIV_TYP_CD);
		Map<String, String> sysMap = null;
		if (CollectionUtils.isNotEmpty(sysList)) {
			sysMap = new HashMap<>(sysList.size());
			for (KeyValueVO dto : sysList) {
				sysMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> livstsMap = null;
		if (CollectionUtils.isNotEmpty(livstsList)) {
			livstsMap = new HashMap<>(livstsList.size());
			for (KeyValueVO dto : livstsList) {
				livstsMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> livtypMap = null;
		if (CollectionUtils.isNotEmpty(livtypList)) {
			livtypMap = new HashMap<>(livtypList.size());
			for (KeyValueVO dto : livtypList) {
				livtypMap.put(dto.getKey(), dto.getValue());
			}
		}
		List<RcCstLivLstVO> rcCstLivLstVOList = RcCstLivLstVOTransform.toVOList(resultList);
		for (RcCstLivLstVO rcCstLivLstVO : rcCstLivLstVOList) {
			if (sysMap != null && sysMap.containsKey(rcCstLivLstVO.getCertTypCd())) {
				rcCstLivLstVO.setCertTypCdNm(sysMap.get(rcCstLivLstVO.getCertTypCd()));
			}
			if (livstsMap != null && livstsMap.containsKey(String.valueOf(rcCstLivLstVO.getIsValid()))) {
				rcCstLivLstVO.setIsValidNm(livstsMap.get(String.valueOf(rcCstLivLstVO.getIsValid())));
			}
			if (livtypMap != null && livtypMap.containsKey(rcCstLivLstVO.getLstTyp())) {
				rcCstLivLstVO.setLstTypNm(livtypMap.get(rcCstLivLstVO.getLstTyp()));
			}
		}
		pager.setResultList(rcCstLivLstVOList);
		return pager;
		//TODO 业务逻辑
//		return null;
	}
}
